package what.basic.progressbar;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Jprogressbarwithtimer implements ActionListener,ChangeListener {
	JFrame f=null;
	JProgressBar pb;
	JLabel lbl;
	Timer timer;
	JButton b;
	public Jprogressbarwithtimer(){
		f=new JFrame("progressbar example");
		Container cp=f.getContentPane();
		lbl=new JLabel("",JLabel.CENTER);
		pb=new JProgressBar();
		pb.setOrientation(JProgressBar.HORIZONTAL);
		pb.setMinimum(0);
		pb.setMaximum(100);
		pb.setValue(0);
		pb.setStringPainted(true);
		pb.addChangeListener(this);
		pb.setPreferredSize(new Dimension(200,30));
		JPanel jp=new JPanel();
		b=new JButton("Start");
		b.addActionListener(this);
		jp.add(b);
		timer=new Timer(50,this);
		cp.add(jp,BorderLayout.NORTH);
		cp.add(pb,BorderLayout.CENTER);
		cp.add(lbl,BorderLayout.SOUTH);
		f.pack();
		f.setVisible(true);
		f.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}
	public static void main(String[] args) {
		new Jprogressbarwithtimer();
	}
	public void actionPerformed(ActionEvent e){
		if(e.getSource()==b){
			timer.start();
		}
		if(e.getSource()==timer){
			int value=pb.getValue();
			if(value<100){
				value++;
				pb.setValue(value);
			}
			else
			{
				timer.stop();
				pb.setValue(0);
			}
		}
	}
	public void stateChanged(ChangeEvent el){
		int value=pb.getValue();
		if(el.getSource()==pb){
			lbl.setText("current progress:"+Integer.toString(value)+"%");
		}
	}

}
